// 设置视频成功分析完成的状态
function setVideoAnalysisStatus(completed) {
    // 将标志存储在localStorage中
    localStorage.setItem('videoAnalysisCompleted', completed.toString());
}

// 获取后端数据
function getBackData(videoname,videoSrc){
    return new Promise((resolve, reject) => {
        // 构建要发送的数据对象
    const requestData = {
        videoname: videoname
    };
    fetch("http://127.0.0.1:8090/init", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(requestData),
    })
      .then(response => {
        if (response.ok) {

          
          return response.json();

        } else {
          reject('请求失败');
        }
      })
      .then(data => {
        var nodejson = data['nodejson'];
        var edgejson = data['edgejson'];
        var treejson = data['treejson'];
        var content = data['content'];
        var summary = data['summary'];


        
        localStorage.setItem("nodejson", JSON.stringify(nodejson));
        localStorage.setItem("edgejson", JSON.stringify(edgejson));
        localStorage.setItem("treejson", JSON.stringify(treejson));
        localStorage.setItem("content", JSON.stringify(content));
        localStorage.setItem("summary", JSON.stringify(summary));


      
        window.location.href = "player.html?videoSrc=" + encodeURIComponent(videoSrc);
   

        

      })
      .catch(error => {
      });
  });
}

document.addEventListener("DOMContentLoaded",function(){
    // 获取链接元素
            
    var videoLink = document.getElementById("videoLink");
    var mainhtml = window.parent.document.getElementById("mainhtml");
    // 添加点击事件监听器
    videoLink.addEventListener("click", function(event) {
        // 阻止默认行为，即打开链接
        setVideoAnalysisStatus(true);
        event.preventDefault();

        var videoSrc = this.getAttribute("href");

        let videoname = this.getAttribute("title")

        getBackData(videoname,videoSrc)
    });
    var videoLink2 = document.getElementById("videoLink2");
    videoLink2.addEventListener("click", function(event) {
        // 阻止默认行为，即打开链接
        setVideoAnalysisStatus(true);

        event.preventDefault();

        var videoSrc = this.getAttribute("href");

        let videoname = this.getAttribute("title")

        getBackData(videoname,videoSrc)
    });
    var videoLink3 = document.getElementById("videoLink3");
    videoLink3.addEventListener("click", function(event) {
        // 阻止默认行为，即打开链接
        setVideoAnalysisStatus(true);

        event.preventDefault();

        var videoSrc = this.getAttribute("href");

        let videoname = this.getAttribute("title")

        getBackData(videoname,videoSrc)
    });
    var videoLink4 = document.getElementById("videoLink4");
    videoLink4.addEventListener("click", function(event) {
        // 阻止默认行为，即打开链接
        setVideoAnalysisStatus(true);

        event.preventDefault();

        var videoSrc = this.getAttribute("href");

        let videoname = this.getAttribute("title")

        getBackData(videoname,videoSrc)
    });
    var videoLink5 = document.getElementById("videoLink5");
    videoLink5.addEventListener("click", function(event) {
        // 阻止默认行为，即打开链接
        setVideoAnalysisStatus(true);

        event.preventDefault();

        var videoSrc = this.getAttribute("href");

        let videoname = this.getAttribute("title")

        getBackData(videoname,videoSrc)
    });
})


